<template>
	<view class="page" :style="loading ? 'opacity: 0;' : 'opacity: 1;'">
		<image src="https://hzybucket.obs.cn-south-1.myhuaweicloud.com/we_chat/top-bg.png" class="bg-color"
			mode="aspectFill"></image>
		<z-paging ref="paging" refresher-only @onRefresh="onRefresh">
			<!-- 头部 -->
			<uv-navbar slot="top" bgColor="rgba(0,0,0,0)" leftIcon=" " :fixed="false" title="全能狮"></uv-navbar>
			<!-- 无数据 -->
			<view slot="empty"></view>
			<!-- 页面内容 -->
			<view class="padidng-box">
				<!-- banner -->
				<uv-swiper v-if="swiperList.length" indicator indicatorStyle="right:24px" bgColor="rgba(0,0,0,0)" radius="16rpx"
					indicatorMode="dot" :list="swiperList"></uv-swiper>
				<!-- add -->
				<view class="xz-bx-box">
					<view class="xz-bx-item" @click="jump('project')">
						<image class="bg" src="../../static/image/xz-bg.png"></image>
						<view class="contnet-xz-bx">
							<text class="txt-xz-bx">新增项目</text>
							<image class="img-xz-bx" src="@/static/image/xz-icon.png"></image>
						</view>
					</view>
					<view class="xz-bx-item" @click="jump('afterSales')">
						<image class="bg" src="../../static/image/bx-bg.png"></image>
						<view class="contnet-xz-bx">
							<text class="txt-xz-bx">售后报修</text>
							<image class="img-xz-bx2" src="@/static/image/bx-icon.png"></image>
						</view>
					</view>
				</view>
				<!-- 案例 -->
				<view class="title-comm-box">
					<view class="title-text">精选案例</view>
					<view class="btn" @click="navToMore">查看全部</view>
				</view>
				<view class="case-list-box" :style="dataList.length ? '' : 'min-height:500rpx;'">
					<template v-for="(item, index) in dataList">
						<view class="case-list-item" :key="index" @click="preFile(item)" v-if="index < 4">
							<view class="icon-box" v-if="item.file_type == 2">
								<uv-icon name="play-right-fill" size="24" color="#ffffff"></uv-icon>
							</view>
							<image v-if="item.file_type == 1" :src="item.case_list[0].file_url" class="case-list-image"
								mode="aspectFill"></image>
							<video v-else :controls="false" :show-fullscreen-btn="false" :show-play-btn="false"
								:show-center-play-btn="false" :src="item.case_list[0].file_url" class="case-list-image"
								mode="aspectFill"></video>
							<view class="case-list-item-title ellipsis">{{ item.title }}</view>
						</view>
					</template>
					<image v-if="!dataList.length" class="no-data" mode="widthFix"
						src="https://hzybucket.obs.cn-south-1.myhuaweicloud.com/we_chat/no-msg2.png"></image>
				</view>
				<!-- 公司介绍 -->
				<view class="title-comm-box">
					<view class="title-text">公司介绍</view>
				</view>
				<view class="rich-text" v-html="introduction"></view>
			</view>
			<!-- 尾部 -->
			<tabbar slot="bottom"></tabbar>
		</z-paging>

		<!-- 视频预览 -->
		<preVideo :show="preVideoShow" :currVideo="currVideo" @close="maskClick"></preVideo>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loading: true,
				scrollTop: '',
				// banner
				swiperList: [],
				// 案例数据
				dataList: [],
				// 公司介绍
				introduction: '',

				currVideo: '',
				preVideoShow: false,
			}
		},
		onLoad() {
			this.getData()
		},
		methods: {
			// ----------------------------跳转----------------------------
			jump(type) {
				if (uni.getStorageSync('userInfo').empower == 0 || !uni.getStorageSync('userInfo')) {
					uni.navigateTo({
						url: '/pages/login/login'
					});
					return;
				}

				if (type == 'project') {
					uni.navigateTo({
						url: '/pages/add_project/add_project'
					});
				} else {
					uni.navigateTo({
						url: '/pages/add_repair/add_repair'
					});
				}
			},
			// ----------------------------案例----------------------------
			navToMore() {
				uni.navigateTo({
					url: '/pages/case_list/case_list'
				});
			},
			preFile(item) {
				if (item.file_type == 1) {
					// 图片
					let urls = [];
					item.case_list.forEach((file) => {
						urls.push(file.file_url);
					});
					uni.previewImage({
						urls
					});
				} else {
					this.preVideoShow = true;
					this.currVideo = item.case_list[0].file_url;
				}
			},
			maskClick() {
				this.preVideoShow = false;
				this.currVideo = '';
			},
			// ----------------------------请求数据----------------------------
			onRefresh() {
				// this.$refs.paging.complete();
				this.getData()
			},
			getData() {
				this.$Api.wxHomepageData().then((res) => {
					console.log(res, '小程序配置数据');
					setTimeout(() => {
						// 停止刷新动画
						this.$refs.paging.complete();
						this.loading = false;
					}, 300);
					if (res.code == 200) {
						let swiperList = [];
						res.data.carousel_image.forEach((item) => {
							swiperList.push(item.file_url);
						});
						this.swiperList = swiperList;

						this.dataList = res.data.case;

						const regex = new RegExp('img', 'gi');
						res.data.introduction.content = res.data.introduction.content.replace(regex,
							`img style="max-width:100%;object-fit: cover;"`);
						this.introduction = res.data.introduction.content;
					}
				}).catch((err) => {
					setTimeout(() => {
						// 停止刷新动画
						this.$refs.paging.complete();
						this.loading = false;
					}, 300);
				});
			}

		}
	}
</script>

<style lang="scss" scoped>
	@import "index.scss";
</style>